package com.longnuoqi.mydoctor.test;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;


import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationListener;
import com.amap.api.maps2d.AMap;
import com.amap.api.maps2d.CameraUpdate;
import com.amap.api.maps2d.CameraUpdateFactory;
import com.amap.api.maps2d.MapView;
import com.amap.api.maps2d.model.BitmapDescriptorFactory;
import com.amap.api.maps2d.model.CameraPosition;
import com.amap.api.maps2d.model.LatLng;
import com.amap.api.maps2d.model.Marker;
import com.amap.api.maps2d.model.MarkerOptions;

import com.amap.api.maps2d.overlay.PoiOverlay;
import com.amap.api.services.core.LatLonPoint;
import com.amap.api.services.core.PoiItem;
import com.amap.api.services.poisearch.PoiResult;
import com.amap.api.services.poisearch.PoiSearch;
import com.yunhe.mydoctor.R;

import java.util.ArrayList;

/**
 * Created by Lenovo on 2015/12/29.
 */
public class TextActivity extends AppCompatActivity implements PoiSearch.OnPoiSearchListener, View.OnClickListener {
    private double longitude;//经度
    private double latitude;//纬度
    private LatLonPoint latLonPoint;
    private EditText keyword;
    private String city;
    private Button search;
    private MapView mapView;
    private AMap amap;
    private PoiSearch.Query query;
    //声明AMapLocationClient类对象
    public AMapLocationClient mlocationClient = null;
    //声明mLocationOption对象,存放的都是定位的一些相关参数
    public AMapLocationClientOption mLocationOption = null;
    //声明定位回调监听器
    public AMapLocationListener mLocationListener = new AMapLocationListener() {

        @Override
        public void onLocationChanged(AMapLocation aMapLocation) {
            if (aMapLocation != null) {
                if (aMapLocation.getErrorCode() == 0) {
                    longitude = aMapLocation.getLongitude();
                    latitude = aMapLocation.getLatitude();
                    latLonPoint = new LatLonPoint(longitude, latitude);
                    city = aMapLocation.getCity();
                    Log.e("=============", "经度：" + longitude + "纬度:" + latitude);
                    Log.e("=============", "国家：" + aMapLocation.getCountry() + "  省：" + aMapLocation.getProvince() + "  城市:"
                            + aMapLocation.getCity() + "  地区" + aMapLocation.getDistrict() + "    街道" + aMapLocation.getRoad());
                    //重写地图中心
                    CameraPosition cp = amap.getCameraPosition();
                    CameraPosition cpnew = CameraPosition.fromLatLngZoom(new LatLng(latitude, longitude), cp.zoom);
                    CameraUpdate update = CameraUpdateFactory.newCameraPosition(cpnew);
                    amap.moveCamera(update);//将地图中心移动到当前位置
                    Marker geoMarker = amap.addMarker(new MarkerOptions().anchor(0.5f, 0.5f));
                    geoMarker.setIcon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_BLUE));

                } else {
                    Log.e("=============", aMapLocation.getErrorInfo());
                }


            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.textlayout);
        initView();
        initLocationOption();
        mapView.onCreate(savedInstanceState);//一定要这句代码
        amap = mapView.getMap();//通过mapview来获取amap对象
    }

    public void initView() {
        keyword = (EditText) findViewById(R.id.keyword);
        search = (Button) findViewById(R.id.search);
        search.setOnClickListener(this);
        mapView = (MapView) findViewById(R.id.map);
    }

    public void initLocationOption() {
        mlocationClient = new AMapLocationClient(this);
        //初始化定位参数
        mLocationOption = new AMapLocationClientOption();
        //设置定位模式为高精度模式，Battery_Saving为低功耗模式，Device_Sensors是仅设备模式
        mLocationOption.setLocationMode(AMapLocationClientOption.AMapLocationMode.Hight_Accuracy);
        //设置是否返回地址信息（默认返回地址信息）
        mLocationOption.setNeedAddress(true);
        //设置是否只定位一次,默认为false
        mLocationOption.setOnceLocation(false);
        //设置是否强制刷新WIFI，默认为强制刷新
        mLocationOption.setWifiActiveScan(true);
        //设置是否允许模拟位置,默认为false，不允许模拟位置
        mLocationOption.setMockEnable(false);
        //设置定位间隔,单位毫秒,默认为2000ms
        mLocationOption.setInterval(2000);
        //给定位客户端对象设置定位参数
        mlocationClient.setLocationOption(mLocationOption);
        mlocationClient.setLocationListener(mLocationListener);
        //启动定位
        mlocationClient.startLocation();
       }



    @Override
    public void onClick(View v) {
        String word = keyword.getText().toString();
        query = new PoiSearch.Query("", word, city);//第一个参数表示的是搜索字符串，第二个参数表示的是搜索类型，第三个表示的是在那个区域内进行搜索
        if (latLonPoint != null) {
            amap.clear();
            PoiSearch poiSearch = new PoiSearch(this, query);
            poiSearch.setOnPoiSearchListener(this);
            //设置搜索区域，以latLonPoint为中心 ，2000米范围内
            poiSearch.setBound(new PoiSearch.SearchBound(latLonPoint, 2, true));
            poiSearch.searchPOIAsyn();//异步搜索

        }
    }
    //POI搜索回调函数

    @Override
    public void onPoiSearched(PoiResult poiResult, int code) {
        if (code == 0) {
            if (poiResult != null && poiResult.getQuery() != null) {
                ArrayList<PoiItem> pois = poiResult.getPois();//得到搜索到的符合条件的数据
                if (pois != null && pois.size() > 0) {
                    PoiOverlay poiOverlay = new PoiOverlay(amap, pois);
                    poiOverlay.addToMap();
                    poiOverlay.zoomToSpan();
                }
            }
        }
    }

    //一定要重写Activity或Fragment对应的生命周期，因为地图属于第三方的提供，不会默认跟随android生命周期进行运作
    @Override
    protected void onResume() {
        super.onResume();
        mapView.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
        mapView.onPause();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mapView.onDestroy();
        mlocationClient.onDestroy();
    }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        mapView.onSaveInstanceState(outState);
    }

}
